require('../scss/vita/vita.scss')
require('../scss/setting.scss')
var set = {
    init: function(){
    	checkTicketStatus(function(ticket){  
                 set.getData(ticket);
            },true,function(){showLoginBox();});
	},
	data:{

	},
	getData: function(ticket){
		$.ajax({
			dataType : 'json'
			,type : 'post'
			,url : 'http://m.yy.com/earn/cherish/queryCherishGroupInfo.action'
			,data : {
				ticket:ticket,
				anchorUid:$.url.param("piUid"),
				source:'2'
			}
			,cache : false
			,timeout : 10000
			,contentType: "application/x-www-form-urlencoded; charset=UTF-8"
			,success : function(data){
				set.data.cherishGroupInfo = data.cherishGroupInfo;
				set.render();
			}
		});

	},
	render: function(){
		var cherishGroupPic = set.data.cherishGroupInfo.cardpic;
		var cardstate = parseInt(set.data.cherishGroupInfo.cardstate);
		var groupnamestate = parseInt(set.data.cherishGroupInfo.groupnamestate);
		var medalstate = parseInt(set.data.cherishGroupInfo.medalstate);
		var level = parseInt(set.data.cherishGroupInfo.lastweekLevel);
		var weekLevel = parseInt(set.data.cherishGroupInfo.weekLevel);
		
		if(cherishGroupPic){
			cherishGroupPic = "http://image.yy.com/earn/"+cherishGroupPic+"?imageview/0/w/116/h/116";
		}else{
			cherishGroupPic = "http://earn.yystatic.com/earn/images/cherish/pc/default1.jpg";
		}
		$("#groupImg").css('background-image','url('+cherishGroupPic+')');
		$("#groupMedalname").val(set.data.cherishGroupInfo.medalname);
		//$("#groupNameS").html(changeFlagName(cherishGroupInfo.medalname,5,true));
		if(medalstate==0){
			if(level>=2||weekLevel>=2){
				$("#medalState").text("只可修改一次");
				$("#medalState").bind('click',function(){
					set.changeEvent();
				});
			}else{
				$("#medalState").text("等级未达到LV2,不可修改");
			}
		}
		if(medalstate==1){
			$("#medalState").text("审核中");
		}
		if(medalstate==2){
			$("#medalState").text("已修改，不可再修改");
		}
		$("#medalState").bind('click',function(){
					set.changeEvent();
				});
		/*if(cardstate==0){
			$("#personCardTips").text("7天内只可修改一次");
		}
		if(cardstate==1){
			$("#personCardTips").text("审核中，不可修改");
		}
		if(cardstate==2){
			$("#personCardTips").text("不可修改，距上次修改不足7天");
		}*/
	},
	changeEvent:function(){
		$("#info-page").hide();
		$("#edit-page").show();
		$("#medalName").change(function(){
			var medalName = $(this).val();
			if(0<medalName.length&&medalName.length<=3&&medalName!=set.data.cherishGroupInfo.medalname){
				$('#edit-page .btn').removeClass('btn-disable').bind('click',function(){
					checkTicketStatus(function(ticket){  
			                 set.save(ticket);
			            },true,function(){showLoginBox();});
				});
			}else{
				$('#edit-page .btn').addClass('btn-disable').unbind();
			}
		});
	},
	save: function(ticket){
		var inList=[];
		inList.push("2");
		inList.push($("#medalName").val());
		if(inList.length >0){
			var param = {};
			for(var i=0;i<inList.length;i+=2){
				param[inList[i]] = inList[i+1];
			}
			param.agentId = $.url.param("piUid");
			param.ticket = ticket;
			$.ajax({
				dataType : 'json'
				,type : 'post'
				,url : 'http://m.yy.com/earn/cherish/modifyCherishGroupInfo.action'
				,data :param
				,cache : false
				,timeout : 10000
				,contentType: "application/x-www-form-urlencoded; charset=UTF-8"
				,success : function(data){
					if(data.result != 0){
						var chTypeName={"1":"团名称","2":"团勋章名称","3":"团名片","4":"团公告"};
						var reStr = [],tStr=[];
						for(var i=0;i<inList.length;i+=2){
							reStr.push(chTypeName[inList[i]]);
							if(inList[i] != 4 && inList[i]!=1){
								tStr.push(chTypeName[inList[i]]);
							}
						}
						var _msg = '恭喜您的'+reStr.join("，")+'提交成功！';
						if(tStr.length>0){
							_msg += '官方会在1-3个工作日内审核，审核通过后才能起效！';
						}
						window.YYApiCore.invokeClientMethod('ui',
							                                'showAlertDialog',
															{
														    "title": "提示",
														    "message": _msg,
														    "buttons":["知道了"],
													        },
															function(resp) {
			
															});
					    $("#info-page").show();
		                $("#edit-page").hide();
						set.init();
					}else{
						var _msg = "操作失败；请稍后重试！";
						if(data.result){
							var re = Number(data.result);
							switch(re){
								case 1:_msg="不是用户本人,操作失败!";break;
								case 9001:_msg="当前你没有珍爱团,操作失败!";break;
								case 9002:_msg="不能修改团勋章-审核中,操作失败!";break;
								case 9003:_msg="不能修改团勋章-不能修改,操作失败!";break;
								case 9004:_msg="不能修改团名片-审核中,操作失败!";break;
								case 9005:_msg="不能修改团名片-一星期修改一次 ,操作失败!";break;
								case 9006:_msg="对不起，团等级不够，不能设置 !";break;
							}
						}
						window.YYApiCore.invokeClientMethod('ui',
							                                'showAlertDialog',
															{
														    "title": "提示",
														    "message": _msg,
														    "buttons":["知道了"],
													        },
															function(resp) {
			
															});
						//$.messagebox({type:'alert', msg:_msg});
						/*
						RESULT_SUCCESS				= 0,		//  = 成功 
						RESULT_NOT_TLOVE_GROUP		= 9001,		//  = 不是珍爱团 
						RESULT_VERIFY_MEDAL			= 9002,		//  = 不能修改团勋章-审核中 
						RESULT_MEDAL_STATE_NOT		= 9003,		//  = 不能修改团勋章-不能修改 
						RESULT_VERIFY_CARD_PIC		= 9004,		//  = 不能修改团名片-审核中 
						RESULT_MODIFY_CARD_LIMIT	= 9005,		//  = 不能修改团名片-一星期修改一次 
						RESULT_GROUP_LEVEL_LIMIT	= 9006,     //  = 等级不够，不能修改 
						*/
						//getUidInfo();
					}
				}
			});
		}

	}
}
set.init();

document.body.addEventListener('touchstart', function () {}); 

